package com.example.demo.controller;

import com.example.demo.model.AppData;
import com.example.demo.model.Constant;
import com.example.demo.model.User;
import com.example.demo.model.json.CommonRootBean;
import com.example.demo.respository.AppRepository;
import com.example.demo.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Optional;

@RestController
@RequestMapping("/app_action")
public class AppController {

    @Autowired
    private AppRepository appRepository;


    /**
     * 保存app
     */
    @PostMapping
    public ModelAndView saveOrUpdateUser(AppData app) {
        System.err.println("saveOrUpdateApp====>>" + app.getName() + "    " + app.getId());
        app = appRepository.save(app);

        if (Constant.TEST_MODE) {
            return new ModelAndView("redirect:/app_action/applist_page");//这是action事件，不是html路径
        } else {
            return new ModelAndView("redirect:/app_action/applist_page");//这是action事件，不是html路径
        }

    }

    /**
     * 获取创建表单页面
     */
    @GetMapping("/form")
    public ModelAndView createForm(Model model) {
        System.err.println("form====>>" + model.toString());
        model.addAttribute("app", new AppData(null, null, null));
        model.addAttribute("title", "创建app");
        return new ModelAndView("blog_zone/app_edit", "appModel", model);
    }

    /**
     * 根据id 查询app
     * modelName  对应html内容页对象解析
     */
    @GetMapping("app_detail01/{id}")
    public ModelAndView getDetail01(@PathVariable("id") Long id, Model model) {
        System.err.println("id====>>" + id);
        Optional<AppData> app = appRepository.findById(id);
        System.err.println("app====>>" + app.get().getName());
        model.addAttribute("app_object", app.get());
        model.addAttribute("title", "查看app");
        return new ModelAndView("blog_zone/app_detail.html", "appModel", model);
    }

    /**
     * 根据id 查询app
     * modelName  对应html内容页对象解析
     * 如果css样式加载失效的话 请调整css目录../css/styles_blog.css====>>/css/styles_blog.css
     */
    @GetMapping("app_detail02/{id}")
    public ModelAndView getDetail02(@PathVariable("id") Long id, Model model) {
        System.err.println("id====>>" + id);
        Optional<AppData> app = appRepository.findById(id);
        CommonUtil.getCommonModel(model);

        String[] image_listbb;
        if (app.get().image_list.isEmpty() ) {
            image_listbb = new String[1];
            image_listbb[0]="";

        } else {
            String images = app.get().image_list;
            image_listbb = images.split(";");
        }


        model.addAttribute("app_object", app.get());
        model.addAttribute("image_list", image_listbb);
        model.addAttribute("title", "查看app");
        return new ModelAndView("blog_zone/app_detail02.html", "dataModel", model);
    }

    /**
     * modelName  对应html内容页对象解析
     */
    //查词所有用户  http://localhost:8090/app_action/applist_page
    @GetMapping("/applist_page")
    public ModelAndView applist_page(Model model) {
        System.err.println("applist_page====>>");
        CommonUtil.getCommonModel(model);

        return new ModelAndView("blog_zone/app_list02.html", "dataModel", model);
    }


    /**
     * String数据
     * 2、通过HttpServletRequest接收    http://localhost:8080/app_action/app_list_data
     *
     * @param request
     * @return success
     */
    @RequestMapping("/app_list_data")
    public String app_list_data(HttpServletRequest request) {
        System.err.println("app_list_data====>>");

        CommonRootBean test = new CommonRootBean();


        Iterable<AppData> applist = appRepository.findAll();
        Iterator iterator = applist.iterator();
        StringBuffer result = new StringBuffer();
        result.append("<div id=\"show_list\" class=\"blogitem\" th:fragment=\"pagination\">\n");
        while (iterator.hasNext()) {
            AppData appData = (AppData) iterator.next();
            if (Constant.TEST_MODE) {
                result.append("<h2 class=\"title\"><a  th:target=\"_blank\" href=\"/app_action/app_detail01/" + appData.getId() + "\">" + appData.getName() + "</a></h2>\n");

            } else {
                result.append("<h2 class=\"title\"><a  th:target=\"_blank\" href=\"/app_action/app_detail02/" + appData.getId() + "\">" + appData.getName() + "</a></h2>\n");
            }


        }
        result.append("</div>");

        test.setCode(0000);
        test.setMsg("测试message");
        test.setData(result.toString());
//        test.setData("<div id=\"show_list\" class=\"blogitem\" th:fragment=\"pagination\">\n" +
//                "    <h2 class=\"title\"><a href=\"/app_action/app_detail02/1\">无线安全U盾</a></h2>\n" +
//                "    <h2 class=\"title\"><a href=\"#\">工厂塑胶回收app</a></h2>\n" +
//                "    <h2 class=\"title\"><a href=\"#\">SunriseMall跨境拼团APP</a></h2>\n" +
//                "    <h2 class=\"title\"><a href=\"#\">Wetok印尼短视频</a></h2>\n" +
//                "    <h2 class=\"title\"><a href=\"#\">出海么 短信群发APP</a></h2>\n" +
//                "</div>");
        return test.getData();
    }

    /**
     * 根据id删除app
     */
    @GetMapping(value = "delete/{id}")
    public ModelAndView delete(@PathVariable("id") Long id) {
        System.err.println("delete====>>" + id);   //fail  不存在会报错
        appRepository.deleteById(id);

        return new ModelAndView("redirect:/app_action/applist_page");//这是action事件，不是html路径
    }

    /**
     * 修改app界面
     */
    @GetMapping(value = "edit/{id}")
    public ModelAndView editForm(@PathVariable("id") Long id, Model model) {
        System.err.println("edit====>>" + id);
        Optional<AppData> app = appRepository.findById(id);
        model.addAttribute("app", app.get());
        model.addAttribute("title", "编辑app");
        return new ModelAndView("blog_zone/app_edit", "appModel", model);
    }

}